<?php
$user_id = $_SESSION['USERID'];

$result = mysql_query("
						SELECT 		`menu_detail`.page_id,
									`menu_detail`.title,
									`menu_detail`.metro_icon,
									`menu_detail`.metro_tile_type
						FROM 		`users`
						LEFT JOIN   `group` ON `group`.id = `users`.`group_id`
						LEFT JOIN   `group_permission` ON `group`.id = `group_permission`.`group_id`
						LEFT JOIN   `menu_detail` ON `group_permission`.`page_id` = `menu_detail`.`page_id`
						WHERE 		`users`.`id` = $user_id AND metro_tile_type != 0
						ORDER BY	`menu_detail`.metro_tile_type DESC
					");
function randomColor() {
	$possibilities = array(1, 2, 3, 4, 5, 6, 7, 8, 9, "A", "B", "C", "D", "E", "F" );
	shuffle($possibilities);
	$color = "#";
	for($i=1;$i<=6;$i++){
		$color .= $possibilities[rand(0,14)];
	}
	return $color;
}

?>

<html>
<head>
		<link href="media/css/main/header.css" rel="stylesheet" type="text/css" />
    	<link href="media/css/main/mainpage.css" rel="stylesheet" type="text/css" />
    	<link href="media/css/main/tooltip.css" rel="stylesheet" type="text/css" />
</head>
<body onselectstart='return false;'>
    <div id="ContentHolder">
     <?php 
       $task_now_count= mysql_fetch_row(mysql_query("   SELECT IF(COUNT(*)>0,CONCAT('<span style=\"color:red;\">',COUNT(*),'</span>'), COUNT(*)),
                                                                      COUNT(*)
                                                              FROM `action`
                                                              WHERE action.actived=1 AND action.end_date >= NOW()"));
               $task_process_count= mysql_fetch_row(mysql_query("   SELECT COUNT(*)
                                                                  FROM `action`
                                                                  WHERE action.actived=1 AND action.end_date < NOW()"));
          
            if($task_now_count[1] > 0){
                $new_task = '<span style="background-color: red; border-radius: 2px 2px 2px 2px; color: #FFFFFF; padding: 1px 2px; position: absolute; top: -5px; right: -6px">NEW</span>';
            }
            $my_task_hide = '';
               if($row[0] < 4){
                    $my_task_hide = '
     <div id="ctl00_ContentPlaceHolder1_tile_Declarations_NEW" class="tile_small" style="background: none repeat scroll 0 0 #1E94DE; position: relative; display: none1; padding: 0 !important; top: 5px;">
        	<p style="display: block!important;margin:8px 0 10px;">აქციები</p>

            '.$new_task.'
        
            <div onclick="location.href=\'index.php?pg=13#tab-0\'" class="tile_waybill_notification tt-wrapper" title="მიმდინარე აქციები" style="float: left; cursor: pointer;">
                <span id="ctl00_ContentPlaceHolder1_lblSavedDecl">'.$task_now_count[0].'</span>
            </div>
            <div onclick="location.href=\'index.php?pg=13#tab-1\'" class="tile_waybill_notification tt-wrapper" title="აქციების არქივი" style="float: left; cursor: pointer;">
                <span id="ctl00_ContentPlaceHolder1_lblSavedDecl">'.$task_process_count[0].'</span>
            </div>
    </div>
                                    ';
                }else {
                    $my_task_hide = "";
                }
                echo $my_task_hide;
        ?>
         <?php 
          function checkgroup($user){
          	$res = mysql_fetch_assoc(mysql_query("
          			SELECT users.group_id
          			FROM    users
          			WHERE  users.id = $user
          			"));
          	return $res['group_id'];
         
          }
         $user_id	= $_SESSION['USERID'];
         $group=checkgroup($user_id);
 		$filter = '';
 		if ($group != 1) {
 			$res_row = mysql_fetch_assoc(mysql_query("SELECT 	users.person_id
 					FROM 	`users`
 					WHERE 	`users`.`id` = $user_id"));
				
 			$filter = 'AND task.responsible_user_id ='. $res_row[person_id];
 		}

             
             $task_now_count1 = mysql_fetch_row(mysql_query("   SELECT IF(COUNT(*)>0,CONCAT('<span style=\"color:red;\">',COUNT(*),'</span>'), COUNT(*)),
                                                                       COUNT(*)
                                                               FROM `task`
                                                               WHERE task.actived=1 AND  task.task_type_id=1 AND task.`status` = 0 $filter"));
             $task_process_count1 = mysql_fetch_row(mysql_query("   SELECT COUNT(*)
                                                                  FROM `task`
                                                                   WHERE task.actived=1 AND task.task_type_id=1 AND task.`status` = 1 $filter"));
             $task_done_count1 = mysql_fetch_row(mysql_query("   SELECT COUNT(*)
                                                                FROM `task`
                                                                WHERE task.actived=1 AND task.task_type_id=1 AND task.`status` = 2 $filter"));
             $task_cancel_count1 = mysql_fetch_row(mysql_query("   SELECT COUNT(*)
                                                                FROM `task`
                                                                WHERE task.actived=1 AND task.task_type_id=1 AND task.`status` = 3 $filter"));
           $row = mysql_fetch_row(mysql_query("SELECT users.group_id
                                                FROM `users`
                                                 WHERE users.id = '$user_id'"));
             if($task_now_count1[1] > 0){
                 $new_task1 = '<span style="background-color: red; border-radius: 2px 2px 2px 2px; color: #FFFFFF; padding: 1px 2px; position: absolute; top: -5px; right: -6px;">NEW</span>';
             }
             $my_task_hide1 = '';
                 if($row[0] != 17){
                     $my_task_hide1 = '
      <div id="ctl00_ContentPlaceHolder1_tile_Declarations_NEW" class="tile_small" style="background: none repeat scroll 0 0 #1E94DE; position: absolute; display: none1; padding: 0 !important; top: 55px; right: 0;">
         <p style="display: block!important;margin:8px 0 10px;">ჩემი გამავალი ზარი</p>

             '.$new_task1.'
        
            <div onclick="location.href=\'index.php?pg=12#tab-0\'" class="tile_waybill_notification tt-wrapper" title="მენეჯერი" style="float: left; cursor: pointer;">
                 <span id="ctl00_ContentPlaceHolder1_lblSavedDecl">'.$task_now_count1[0].'</span>
             </div>
             <div onclick="location.href=\'index.php?pg=12#tab-1\'" class="tile_waybill_notification tt-wrapper" title="პირველადი" style="float: left; cursor: pointer;">
                 <span id="ctl00_ContentPlaceHolder1_lblSavedDecl">'.$task_process_count1[0].'</span>
             </div>
             <div onclick="location.href=\'index.php?pg=12#tab-2\'" class="tile_waybill_notification tt-wrapper" title="მიმდინარე" style="float: left; margin-top: 5px; cursor: pointer;">
                 <span id="ctl00_ContentPlaceHolder1_lblSavedDecl">'.$task_done_count1[0].'</span>
             </div>
             <div onclick="location.href=\'index.php?pg=12#tab-3\'" class="tile_waybill_notification tt-wrapper" title="დასრულებული" style="float: left; margin-top: 5px; cursor: pointer;">
                 <span id="ctl00_ContentPlaceHolder1_lblSavedDecl">'.$task_cancel_count1[0].'</span>
             </div>
     </div>
                                     ';
                 }else {
                     $my_task_hide1 = "";
                }
                 echo $my_task_hide1;
         ?>
    <div class="content">
        <table class="tiles">
            <tbody>
                <tr>
                    <td>
                    <?php
						$count =  round(mysql_num_rows($result) / 3);

						for ($i = 1; $i < $count - 2; $i++) {
							$row = mysql_fetch_assoc($result);
							if ($row[metro_tile_type] == 1) {
								echo '
									<div  class="tile_small" style="background: #'.dechex(rand(0x220000, 0xFFFFFF)).'; box-shadow: 2px 5px 5px #ccc;" onclick="location.href=\'index.php?pg='.$row[page_id].'\'">
			                            <p style="font-size: 14px;">'.$row[title].'</p>
			                        </div>
									';
							}elseif ($row[metro_tile_type] == 2) {
								echo '
									<div  class="tile_large"  style="background: #'.dechex(rand(0x200000, 0xFFFFFF)).'; box-shadow: 2px 5px 5px #ccc;" onclick="location.href=\'index.php?pg='.$row[page_id].'\'">
										<div class="tile_icon" style="margin-top: 10px;">
											<img src="media/images/main/'.$row[metro_icon].'" alt="" style="background-position: -116px -18px; width: 50px; height: 50px;" />
										</div><p style="margin-top: 22px; margin-left: 80px">'.$row[title].'</p>
									</div>
									';
							}
						}
                    ?>
                    </td>

                    <td>
                    <?php

						for ($i = $count; $i < $count + $count +2; $i++) {
							$row = mysql_fetch_assoc($result);
							if ($row[metro_tile_type] == 1) {
								echo '
									<div  class="tile_small" style="background: #'.dechex(rand(0x200000, 0xFFFFFF)).'; box-shadow: 2px 5px 5px #ccc;" onclick="location.href=\'index.php?pg='.$row[page_id].'\'">
			                            <p style="font-size: 14px;">'.$row[title].'</p>
			                        </div>
									';
							}elseif ($row[metro_tile_type] == 2) {
								echo '
									<div  class="tile_large"  style="background:#'.dechex(rand(0x200000, 0xFFFFFF)).'; box-shadow: 2px 5px 5px #ccc;" onclick="location.href=\'index.php?pg='.$row[page_id].'\'">
										<div class="tile_icon" style="margin-top: 10px;">
											<img src="media/images/main/'.$row[metro_icon].'" alt="" style="background-position: -116px -18px; width: 50px; height: 50px;" />
										</div><p style="margin-top: 22px; margin-left: 80px">'.$row[title].'</p>
									</div>
									';
							}
						}
                    ?>
                    </td>

                    <td>
                    <?php
						for ($i = $count + $count + 2; $i <= mysql_num_rows($result); $i++) {
							$row = mysql_fetch_assoc($result);
							if ($row[metro_tile_type] == 1) {
								echo '
									<div  class="tile_small" style="background:#'.dechex(rand(0x200000, 0xFFFFFF)).'; box-shadow: 2px 5px 5px #ccc;" onclick="location.href=\'index.php?pg='.$row[page_id].'\'">
			                            <p style="font-size: 14px;">'.$row[title].'</p>
			                        </div>
									';
							}elseif ($row[metro_tile_type] == 2) {
								echo '
									<div  class="tile_large"  style="background: #'.dechex(rand(0x200000, 0xFFFFFF)).'; box-shadow: 2px 5px 5px #ccc;" onclick="location.href=\'index.php?pg='.$row[page_id].'\'">
										<div class="tile_icon" style="margin-top: 10px;">
											<img src="media/images/main/'.$row[metro_icon].'" alt="" style="background-position: -116px -18px; width: 50px; height: 50px;" />
										</div><p style="margin-top: 22px; margin-left: 80px">'.$row[title].'</p>
									</div>
									';
							}
						}
                    ?>
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
        </div>
</body>
</html>
